package com.jzx.provider.exception;

import com.jzx.provider.common.result.InvokeResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * @author
 * @date 2018/7/27
 */
@RestControllerAdvice
@Slf4j
public class MyExceptionHandler {

    @ExceptionHandler(MyRuntimeException.class)
    public InvokeResult<StackTraceElement> handleHyCloudRuntimeException(MyRuntimeException exception) {
        log.error("<<<<<<<" + exception.getMessage() + ":{}", exception);
        return InvokeResult.error(exception);
    }

    /**
     * 参数校验错误拦截处理
     *
     * @param exception 错误信息集合
     * @return 错误信息
     */
    @ExceptionHandler(MethodArgumentNotValidException.class)
    public InvokeResult validationBodyException(MethodArgumentNotValidException exception) {
        log.error("----------->" + exception.getMessage() + ":{}", exception);
        String message = exception.getBindingResult().getAllErrors().get(0).getDefaultMessage();
        return InvokeResult.error(message, InvokeResult.TYPE_FAIL_PARAM);
    }
}
